import { fileURLToPath, URL } from 'node:url'
const path = require('path')
const fs = require('fs')

import { defineConfig } from 'vite'
import vue2 from '@vitejs/plugin-vue2'

const moment = require('moment')
const childProcess = require('child_process')
const buildTime = `${moment(new Date()).format('YYYYMMDD-HHmmss')}`
const version = childProcess
  .execSync('git rev-parse --abbrev-ref HEAD')
  .toString()
  .replace(/\s+/, '')
  .split('-')
  .pop()
  .split('/')
  .pop()
process.env.VITE_BUILD_TIME = buildTime
process.env.VITE_BUILD_VERSION = version

fs.writeFile(path.resolve(`./public/buildTime.txt`), buildTime, (err: any) => {
  console.log(err ?? 'build标记成功')
})
const isProd = process.env.VITE_ENV == 'prod'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue2()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    extensions: ['.js', '.ts', '.vue']
  },
  esbuild: {
    drop: isProd ? ['console', 'debugger'] : []
  },
  css: {
    postcss: {
      plugins: [
        {
          postcssPlugin: 'postcss-tansform-transform',
          Declaration(decl) {
            if (decl.prop == 'tansform') decl.prop = 'transform'
          }
        }
      ]
    }
  }
})
